﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace MatrixWebApiCore.Common
{
    public class EnumItem
    {
        /// <summary>
        /// 枚举描述
        /// </summary>
        public string Description { get; set; }
        /// <summary>
        /// 枚举英文名称
        /// </summary>
        public string Name { get; set; }
        /// <summary>
        /// 枚举整型值
        /// </summary>
        public int Value { get; set; }
    }
    public class EnumHelpter
    {
        /// <summary>
        /// 从枚举类型和它的特性读出并返回一个键值对
        /// </summary>
        /// <param name="enumType">Type,该参数的格式为typeof(需要读的枚举类型)</param>
        /// <returns>键值对</returns>
        public static List<EnumItem> GetNVCFromEnumValue(Type enumType)
        {
            List<EnumItem> list = new List<EnumItem>();
            foreach (var item in Enum.GetValues(enumType))
            {
                int value = Convert.ToInt32(item);
                string description = string.Empty;
                //获取描述
                FieldInfo field = item.GetType().GetField(item.ToString());
                object[] objs = field.GetCustomAttributes(typeof(DescriptionAttribute), false);    //获取描述属性
                if (objs.Length == 0)    //当描述属性没有时，直接返回名称
                    description = item.ToString();
                DescriptionAttribute descriptionAttribute = (DescriptionAttribute)objs[0];
                //description = descriptionAttribute.Description;
                list.Add(new EnumItem() {Name= item.ToString() ,Description= descriptionAttribute.Description ,Value= value });
            }
            return list;             
        }


    }
}
